//
// $Id$

package zachg.survival;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Polygon;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

import com.samskivert.swing.GroupLayout;
import com.samskivert.swing.MultiLineLabel;
import com.samskivert.swing.ShapeIcon;

import com.threerings.crowd.client.PlacePanel;
import com.threerings.util.MessageBundle;

import com.threerings.toybox.client.ChatPanel;
import com.threerings.toybox.util.ToyBoxContext;
import com.threerings.toybox.client.ToyBoxUI;

/**
 * Contains the primary client interface for the game.
 */
@SuppressWarnings("serial")
public class SurvivalPanel extends PlacePanel
{
	
	/**
     * Creates a Survival panel and its associated interface components.
     */
	public SurvivalPanel (ToyBoxContext ctx, final SurvivalController ctrl)
    {
        super(ctrl);
        _ctx = ctx;

        // this is used to look up localized strings
        MessageBundle msgs = _ctx.getMessageManager().getBundle("survival");

        // give ourselves a wee bit of a border
        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        setLayout(new BorderLayout());
        
        // give ourself a soothing blue background
        setBackground(new Color(0x6699CC));        

        // create a container that will hold our board view in its center
        JPanel box = GroupLayout.makeHBox();
        // create and add our board view
        box.add(bview = new SurvivalBoardView(ctx, ctrl));
        box.setOpaque(false);
        add(box, BorderLayout.CENTER);
        
        // create a tabbed side panel to hold our chat and other extra interfaces
        JTabbedPane tabbedSidePanel =  new JTabbedPane(); //GroupLayout.makeVStretchBox(5);
        tabbedSidePanel.setOpaque(false);
        
        //create our tabs
        tabbedSidePanel.addTab("Game", createGamePanel(msgs, ctrl));
        tabbedSidePanel.add("Instructions",createHelpPanel(msgs));
        tabbedSidePanel.add("Chat",createChatPanel(msgs));        
        
        // add our side panel to the main display
        add(tabbedSidePanel, BorderLayout.EAST); 
    }
    
    /**
     * Creates a panel with all the game indicators, buttons and feedback
     *
     * @param msgs the msgs
     * @param ctrl the ctrl
     * @return the Jpanel
     */
    @SuppressWarnings("deprecation")
    private JPanel createGamePanel(MessageBundle msgs, final SurvivalController ctrl)
    {     
        // create a side panel to hold our chat and other extra interfaces
        JPanel sidePanel = GroupLayout.makeVStretchBox(5);
        sidePanel.setOpaque(false);
                
        // add a big fat label
        MultiLineLabel vlabel = new MultiLineLabel(msgs.get("m.title"));
        //TODO: figure out what replacement is for setAntiAliased method
        vlabel.setAntiAliased(true);
        vlabel.setFont(ToyBoxUI.fancyFont);        
        sidePanel.add(vlabel, GroupLayout.FIXED);
        
        // current turn count
        turnCountLabel = new JLabel("Turn 0");
        currentHealth = new JLabel("Health: ");
        sidePanel.add(turnCountLabel, GroupLayout.FIXED);
        sidePanel.add(currentHealth, GroupLayout.FIXED);
        
        //add action options
        
        JLabel actionOptionslabel = new JLabel("Action Options");
        actionOptionslabel.setFont(new Font("Helvetica", Font.BOLD, 24));
        actionOptionslabel.setForeground(Color.black);
        
        attackButton = new JButton("Attack");
        attackButton.addMouseListener(new MouseAdapter() {
        	public void mousePressed (MouseEvent e) {
        			ctrl.gameCharacterAttack();
        		}
            });
        mineButton = new JButton("Mine");
        mineButton.addMouseListener(new MouseAdapter() {
        	public void mousePressed (MouseEvent e) {
        			ctrl.gameCharacterMined();
        		}
            });        
        doNothing = new JButton("Do Nothing");
        doNothing.addMouseListener(new MouseAdapter() {
        	public void mousePressed (MouseEvent e) {
        			ctrl.endTurn();
        		}
            });   
        JLabel mineInfo = new JLabel("Move over a grey square to mine the area.");
        mineInfo.setFont(new Font("Helvetica", Font.BOLD, 10));
        mineInfo.setForeground(Color.black);        
        JLabel attackInfo = new JLabel("Move 1 square from another unit to attack.");
        attackInfo.setFont(new Font("Helvetica", Font.BOLD, 10));
        attackInfo.setForeground(Color.black);        
        
        
        sidePanel.add(actionOptionslabel, GroupLayout.FIXED);
        sidePanel.add(attackButton, GroupLayout.FIXED);
        sidePanel.add(attackInfo, GroupLayout.FIXED);
        sidePanel.add(mineButton, GroupLayout.FIXED);
        sidePanel.add(mineInfo, GroupLayout.FIXED);
        sidePanel.add(doNothing, GroupLayout.FIXED);
        
        //Add info box for rollovers on units
        JLabel unitInfoLabel = new JLabel("Unit Information");
        unitInfoLabel.setFont(new Font("Helvetica", Font.BOLD, 24));
        unitInfoLabel.setForeground(Color.black);
        
        JLabel unitInfoLabelDetails = new JLabel("Roll mouse over a unit to view information about it");
        unitInfoLabelDetails.setFont(new Font("Helvetica", Font.BOLD, 10));
        unitInfoLabelDetails.setForeground(Color.black);
        
        unitInfoDetails = new JTextArea("No unit selected \n \n \n \n");
        unitInfoDetails.setFont(new Font("Helvetica", Font.BOLD, 10));
        unitInfoDetails.setForeground(Color.black);
        unitInfoDetails.setBackground(new Color(0x6699CC));
        unitInfoDetails.setOpaque(true);
        unitInfoDetails.setSize(75, 50);
        unitInfoDetails.setEditable(false);
        
        sidePanel.add(unitInfoLabel, GroupLayout.FIXED);
        sidePanel.add(unitInfoLabelDetails, GroupLayout.FIXED);
        sidePanel.add(unitInfoDetails, GroupLayout.FIXED);

        //add game progress indicators (turn display)
        JLabel gameSituation = new JLabel("Game Progress");
        gameSituation.setFont(new Font("Helvetica", Font.BOLD, 24));
        gameSituation.setForeground(Color.black);
        sidePanel.add(gameSituation, GroupLayout.FIXED);
        
        // add a standard turn display 
        //At initialization, the controller isn't fully started, so we need to defer creation of icons.  
        //This occurs in the startup method, willEnterPlace of the view
        turnDisplay = new SurvivalTurnDisplay();
        turnDisplay.setOpaque(false);
        Polygon triangle = new Polygon(new int[] { 0, 12, 0 },
                                       new int[] { 0, 6, 12 }, 3);
        turnDisplay.setTurnIcon(new ShapeIcon(triangle, Color.yellow, null));
        turnDisplay.setWinnerText(_ctx.xlate("reversi", "m.winner"));
        turnDisplay.setDrawText(_ctx.xlate("reversi", "m.draw"));
        sidePanel.add(turnDisplay, GroupLayout.FIXED);     

        // add a "back to lobby" button
        JButton back = SurvivalController.createActionButton(
            msgs.get("m.back_to_lobby"), "backToLobby");
        sidePanel.add(back, GroupLayout.FIXED);        
        
        return sidePanel;
    }    
    
    /**
     * Creates a simple panel with a chat box
     *
     * @param msgs the msgs
     * @return the Jpanel
     */
    @SuppressWarnings("deprecation")
    private JPanel createChatPanel(MessageBundle msgs)
    {
        JPanel chatPanel = GroupLayout.makeVStretchBox(5); 
        chatPanel.setOpaque(false);

        MultiLineLabel vlabel = new MultiLineLabel(msgs.get("m.title"));
        //TODO: figure out what replacement is for setAntiAliased method
        vlabel.setAntiAliased(true);
        vlabel.setFont(ToyBoxUI.fancyFont);        
        chatPanel.add(vlabel, GroupLayout.FIXED);        
        
        JLabel chatInformationTitle = new JLabel("Chat");
        chatInformationTitle.setFont(new Font("Helvetica", Font.BOLD, 24));
        chatInformationTitle.setForeground(Color.black);
        chatPanel.add(chatInformationTitle, GroupLayout.FIXED);

        chatPanel.add(new ChatPanel(_ctx));

        JButton back = SurvivalController.createActionButton(
            msgs.get("m.back_to_lobby"), "backToLobby");
        chatPanel.add(back, GroupLayout.FIXED);
        
        return chatPanel;
    }
    
    /**
     * Creates a panel specifically for the game instructions
     *
     * @param msgs the msgs
     * @return the Jpanel
     */
    @SuppressWarnings("deprecation")
    private JPanel createHelpPanel(MessageBundle msgs)
    {
        JPanel helpPanel = GroupLayout.makeVStretchBox(5); 
        helpPanel.setOpaque(false);
        
        MultiLineLabel vlabel = new MultiLineLabel(msgs.get("m.title"));
        //TODO: figure out what replacement is for this method
        vlabel.setAntiAliased(true);
        vlabel.setFont(ToyBoxUI.fancyFont);        
        helpPanel.add(vlabel, GroupLayout.FIXED);
        
        JLabel helpInformationTitle = new JLabel("Game Instructions");
        helpInformationTitle.setFont(new Font("Helvetica", Font.BOLD, 24));
        helpInformationTitle.setForeground(Color.black);
        helpPanel.add(helpInformationTitle, GroupLayout.FIXED);
        
        JTextArea helpInformation = new JTextArea("");
        helpInformation.setFont(new Font("Helvetica", Font.BOLD, 10));
        helpInformation.setForeground(Color.black);
        helpInformation.setBackground(new Color(0x6699CC));
        helpInformation.setEditable(false);
        helpInformation.setLineWrap(true);
        helpInformation.setWrapStyleWord(true);
        helpInformation.setText(
        		"Attack and mine to earn points, winner is the player with the most points after all turns have run out. \n \n"
        		
        		+"To move, click a square near where your character is. Look at the turn display to see what color you are, the circle on the board with that color is you. Each turn, you can move 3 squares from your current position. \n \n"
        		
        		+"After deciding where to move (or not to at all), you have to click one of the three buttons in the Game tab of the panel on the right to complete your turn. \n \n"
        		
        		+"To attack, move within 1 square of a creep or another player and press the attack button. \n \n"
        		
        		+"Every time you kill a creep or another player, you will do more damage and earn more points from mining or attacking. \n \n"
        		
        		+"To mine, move on top of any of the darker squares on the board and press Mine. \n \n"
        		
        		+"Every time that there's a full rotation of players, a new creep will appear on the board. \n \n"
        		
        		+"Creeps will move towards players and attack them. \n \n"
        		
        		+"Once in a while, an act of god will come and kill the player with the most health. \n \n"
        		
        		+"Will you help your friends and protect each other from creeps as you earn points? Or will you kill each other for individual gain? \n \n"
        
        );
        helpPanel.add(helpInformation);

        JButton back = SurvivalController.createActionButton(
                msgs.get("m.back_to_lobby"), "backToLobby");
        helpPanel.add(back, GroupLayout.FIXED);
        
        return helpPanel;
    }    

    /** Provides access to various client services. */
    protected ToyBoxContext _ctx;
    
    /** The board view. */
    public SurvivalBoardView bview;

    public JLabel turnCountLabel;
    public JLabel currentHealth;    
    public JLabel currentLeader;
    public SurvivalTurnDisplay turnDisplay;
    public JTextArea unitInfoDetails;
    public JButton attackButton;
    public JButton mineButton;
    public JButton doNothing;
}
